package com.ruoyi.erecord.vehicles.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.erecord.vehicles.domain.VehiclesImportDetail;
import com.ruoyi.erecord.vehicles.domain.bo.VehiclesImportDetailBo;
import com.ruoyi.erecord.vehicles.domain.vo.VehiclesImportDetailVo;
import com.ruoyi.erecord.vehicles.mapper.VehiclesImportDetailMapper;
import com.ruoyi.erecord.vehicles.service.IVehiclesImportDetailService;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;

/**
 * 车辆导入明细Service业务层处理
 *
 * @author cc
 * @date 2023-04-18
 */
@RequiredArgsConstructor
@Service
public class VehiclesImportDetailServiceImpl implements IVehiclesImportDetailService {

    private final VehiclesImportDetailMapper baseMapper;

    /**
     * 查询车辆导入明细
     */
    @Override
    public VehiclesImportDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询车辆导入明细列表
     */
    @Override
    public TableDataInfo<VehiclesImportDetailVo> queryPageList(VehiclesImportDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<VehiclesImportDetail> lqw = buildQueryWrapper(bo);
        Page<VehiclesImportDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询车辆导入明细列表
     */
    @Override
    public List<VehiclesImportDetailVo> queryList(VehiclesImportDetailBo bo) {
        LambdaQueryWrapper<VehiclesImportDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<VehiclesImportDetail> buildQueryWrapper(VehiclesImportDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<VehiclesImportDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), VehiclesImportDetail::getBatchNo, bo.getBatchNo());
        lqw.eq(StringUtils.isNotBlank(bo.getVehiclesCode()), VehiclesImportDetail::getVehiclesCode, bo.getVehiclesCode());
        lqw.like(StringUtils.isNotBlank(bo.getVehiclesName()), VehiclesImportDetail::getVehiclesName, bo.getVehiclesName());
        lqw.eq(StringUtils.isNotBlank(bo.getVehiclesNumber()), VehiclesImportDetail::getVehiclesNumber, bo.getVehiclesNumber());
        lqw.eq(bo.getProcessStatus() != null, VehiclesImportDetail::getProcessStatus, bo.getProcessStatus());
        return lqw;
    }

    /**
     * 新增车辆导入明细
     */
    @Override
    public Boolean insertByBo(VehiclesImportDetailBo bo) {
        VehiclesImportDetail add = BeanUtil.toBean(bo, VehiclesImportDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改车辆导入明细
     */
    @Override
    public Boolean updateByBo(VehiclesImportDetailBo bo) {
        VehiclesImportDetail update = BeanUtil.toBean(bo, VehiclesImportDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(VehiclesImportDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除车辆导入明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

	@Override
	public boolean batchInsertBo(List<VehiclesImportDetailVo> voList) {
		List<VehiclesImportDetail> list= BeanCopyUtils.copyList(voList , VehiclesImportDetail.class);
		return baseMapper.insertBatch(list);
	}

	@Override
	public Boolean batchUpdatetBo(List<VehiclesImportDetailVo> voList) {
		List<VehiclesImportDetail> list= BeanCopyUtils.copyList(voList , VehiclesImportDetail.class);
		return baseMapper.updateBatchById(list);
	}
}
